package com.itheima.leyou.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class StringUtil {
    public static long getDifferentTime(String nowTime,String endTime){
        //当前时间初始化
        SimpleDateFormat formatBegin = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //秒杀结束时间初始化
        SimpleDateFormat formatEnd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date beginTimeD = null;
        Date endTimeD = null;
        try {
            beginTimeD = formatBegin.parse(nowTime);
            endTimeD = formatEnd.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //取秒杀结束时间与时间服务器当前时间相差秒数，作为redis的有效期
        long dayTime = (beginTimeD.getTime()-endTimeD.getTime())/(1000);
        return dayTime;
    }

    public static Map<String,Object> mapStringToMap(String str){
//        str=str.substring(1, str.length()-1);
        str = str.replace("{", "");
        str = str.replace("}", "");
        String[] strs=str.split(",");
        Map<String,Object> map = new HashMap<String, Object>();
        for (String string : strs) {
            String key=string.split("=")[0].trim();
            String value=string.split("=")[1];
            map.put(key, value);
        }
        return map;
    }
}
